#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+5;
#define P pair<int,int>
#define px first
#define py second
int n,m,T,a_t[N],a[N],c[N],x[4];
inline int calc_k(const int& x){
    return 1<<__lg(x)==x?__lg(x):__lg(x)+1;
}
bitset<N> r[30];
signed main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a_t[i];
    for(int i=1;i<=m;i++) cin>>c[i];
    int k=calc_k(n);
    string s;
    for(int i=1;i<=k;i++){
        cin>>s;
        for(int j=0;j<s.size();j++) r[i][j+1]=(s[j]=='1');
    }
    cin>>T;
    while(T--){
        for(int& i:x) cin>>i;
        for(int i=1;i<=n;i++) a[i]=a_t[i]^x[i%4];
        ll ans=0;
        for(int i=1;i<=m;i++){
            int k=1<<calc_k(i);
            vector<int> t,t2;
            for(int j=1;j<=k;j++) t.push_back(i);
            int round=1;
            while(t.size()>1){
                t2.clear();
                for(int j=0;j<t.size();j+=2){
                    if(r[round][(j>>1)+1]) swap(t[j],t[j+1]);
                    t2.push_back(a[t[j]]>=round?t[j]:t[j+1]);
                }
                t=t2;
                ++round;
            }
            ans^=(ll)i*t[0];
        }
        cout<<ans<<endl;
    }
    return 0;
}
